package com.qidian.buy.search.config;

import com.qidian.buy.search.receives.ItemIdReceive;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: 戴着假发的程序员
 * Date: 2024-05-15 14:44
 * company: 起点编程
 **/
@Configuration
public class ItemOptionListenerConfig {
    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;
    @Autowired
    private ItemIdReceive itemReceive;
    // 配置一个listener
    @Bean
    public SimpleMessageListenerContainer listenerContainer(){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cachingConnectionFactory);
        // 监听队列名
        container.setQueueNames("item-search");  // 可以在配置文件中配置
        // 当前消费者数量
        container.setConcurrentConsumers(1); // 建议在配置文件中配置
        // 最大消费者数量
        container.setMaxConcurrentConsumers(1);// 建议在配置文件中配置
        // 手动确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        // 设置监听器
        container.setMessageListener(itemReceive);
        return container;
    }
}
